package com.open.biz.config.impl;

import com.open.base.functions.IEnabledOperation;
import com.open.biz.config.Config;
import com.open.biz.config.ConfigSourceProvider;
import com.open.biz.config.entity.ProductParameterInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ProductParameterInfoConfigImpl implements
        Config<ProductParameterInfo>,
        ConfigSourceProvider<List<ProductParameterInfo>>,
        IEnabledOperation<ProductParameterInfo> {

    /**
     * 编写 enable 规则
     *
     * @return
     */
    @Override
    public List<ProductParameterInfo> enableOperation(ProductParameterInfo... resource) {
        List<ProductParameterInfo> parameterInfos = Arrays.asList(resource);
        return parameterInfos
                .stream()
                .filter(productParameterInfo -> IsDel.AVAILABLE.ordinal() == productParameterInfo.getIsDel())
                .collect(Collectors.toList());
    }

    private enum IsDel {
        AVAILABLE,
        UNAVAILABLE,
    }


    @Override
    public int ordinal() {
        return 0;
    }

    @Override
    public ProductParameterInfo read(final ProductParameterInfo productParameterInfo) {
        Optional<ProductParameterInfo> any = enableOperation(source().toArray(new ProductParameterInfo[0]))
                .stream()
                .filter(item -> item.getConfigName().equals(productParameterInfo.getConfigName()))
                .findAny();
        return any.orElse(null);
    }

    @Override
    public List<ProductParameterInfo> readList(ProductParameterInfo productParameterInfo) {
        return enableOperation(source().toArray(new ProductParameterInfo[0]))
                .stream()
                .filter(item -> item.getConfigName().equals(productParameterInfo.getConfigName()))
                .collect(Collectors.toList());
    }


    @Override
    public List<ProductParameterInfo> source() {
        ProductParameterInfo record = new ProductParameterInfo();
        record.setConfigName("config1");
        record.setIsDel(IsDel.AVAILABLE.ordinal());

        ProductParameterInfo record2 = new ProductParameterInfo();
        record2.setConfigName("config2");
        record2.setIsDel(IsDel.UNAVAILABLE.ordinal());

        ArrayList<ProductParameterInfo> resource = new ArrayList<>();
        resource.add(record);
        resource.add(record2);
        return resource;
    }
}
